package com.elinkway.infinitemovies.utils;

import android.content.Context;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.bean.UMengReport;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;

public class UmengEventPoster {
    private static final String TAG = "UmengEventPoster";
    /**
     * ENABLE 是否上报
     */
    public static final Boolean ENABLE = true;

    /**
     * 播放器播放结果枚举值
     */
    public static final String PLAYER_RESULT_SUCCESS = "success";
    public static final String PLAYER_RESULT_30_CANCEL = "30cancel";
    public static final String PLAYER_RESULT_30_MORE_CANCEL = "30morecancel";
    public static final String PLAYER_RESULT_FAILED = "failed";

    /**
     * 播放器来源页面枚举值
     */
    public static final String PLAYER_FROM_DETAIL = "detailpage";
    public static final String PLAYER_FROM_HISTORY = "history";

    /**
     * 频道点击页面枚举值
     */
    public static final String CHANNEL_MOVIE = "movie";
    public static final String CHANNEL_TV = "tv";
    public static final String CHANNEL_CARTOON = "cartoon";
    public static final String CHANNEL_VARIETY = "variety";
    public static final String CHANNEL_HISTORY = "history";
    public static final String CHANNEL_FAVOR = "favor";
    public static final String CHANNEL_SEARCH = "search";
    public static final String CHANNEL_SETTING = "setting";

    /**
     * 推送类型枚举值
     */
    public static final String PUSH_TYPE_ALBUM = "album";
    public static final String PUSH_TYPE_THEME = "theme";
    public static final String PUSH_TYPE_LINK = "link";
    public static final String PUSH_TYPE_OTHER = "other";
    /**
     * 预留下版本
     */
    public static final String PUSH_TYPE_VIDEO = "videoplay";

    /**
     * 登录相关点击统计
     */
    public static final String CLICK_TO_LOGIN = "click_to_login";
    public static final String CLICK_USER_INFO = "click_user_info";
    public static final String CLICK_WEIBO_LOGIN = "click_weibo_login";
    public static final String CLICK_WEIXIN_LOGIN = "click_weixin_login";
    public static final String CLICK_QQ_LOGIN = "click_qq_login";
    public static final String EDIT_AVATAR = "edit_avatar";
    public static final String EDIT_NICKNAME = "edit_nickname";
    public static final String EDIT_GENDER = "edit_gender";

    /**
     * 访问页面相关的统计
     */
    public static final String VISIT_VARIETY = "page_show_variety";
    public static final String VISIT_MOVIES = "page_show_movie";
    public static final String VISIT_CARTOON = "page_show_cartoon";
    public static final String VISIT_TV_PALY = "page_show_episodes";
    public static final String VISIT_RECORD = "page_show_documentary";
    public static final String VISIT_TOPIC_LIST = "page_show_topic";
    public static final String VISIT_SEARCH = "page_show_search";
    public static final String VISIT_RECOMMEND = "page_show_home";
    public static final String VISIT_CHANNEL = "page_show_category";
    public static final String VISIT_RANK = "page_show_ranking";
    public static final String VISIT_FAVORITE = "page_show_collect";
    public static final String VISIT_HISTORY = "page_show_history";
    public static final String VISIT_DOWNLOAD = "page_show_download";
    public static final String VISIT_VIDEO_DETAIL = "page_show_details";
    public static final String VISIT_TOPIC_DETAIL = "page_show_topic_detail";

    /**
     * 影片观看时长
     */
    public static final String WATCH_TIME = "timespending";

    /**
     * H5页分享统计
     */
    public static final String H5_SHARE = "share_html";

    /**
     * 离开页面相关的统计
     */
    public static final String LEAVE_VARIETY = "page_leave_variety";
    public static final String LEAVE_MOVIES = "page_leave_movie";
    public static final String LEAVE_CARTOON = "page_leave_cartoon";
    public static final String LEAVE_TV_PALY = "page_leave_episodes";
    public static final String LEAVE_RECORD = "page_leave_documentary";
    public static final String LEAVE_TOPIC_LIST = "page_leave_topic";
    public static final String LEAVE_SEARCH = "page_leave_search";
    public static final String LEAVE_RECOMMEND = "page_leave_home";
    public static final String LEAVE_CHANNEL = "page_leave_category";
    public static final String LEAVE_RANK = "page_leave_ranking";
    public static final String LEAVE_MY = "page_leave_my";
    public static final String LEAVE_HOT = "page_leave_hot";
    public static final String LEAVE_FAVORITE = "page_leave_collect";
    public static final String LEAVE_HISTORY = "page_leave_history";
    public static final String LEAVE_DOWNLOAD = "page_leave_download";
    public static final String LEAVE_VIDEO_DETAIL = "page_leave_details";

    /**
     * liteapp相关上报
     */
    public static final String LITEAPP_INSTALL_PROMOTE = "liteapp_install_promote";
    public static final String LITEAPP_INVOKE = "liteapp_invoke";
    //退出广告的相关统计
    public static final String EXIT_DIALOG_YES = "exit_dialog_yes";
    public static final String EXIT_DIALOG_NO = "exit_dialog_no";
    public static final String CLICK_EXIT_AD = "click_exit_ad";

    public static final String ALBUM_PLAY_DEFAULT_STATE = "album_detail_play_btn_state";
    public static final String ALBUM_PLAY_OFF = "off";
    public static final String ALBUM_PLAY_H5 = "h5";
    public static final String ALBUM_PLAY = "play";
    public static final String PLAYER_PAUSE = "player_pause";
    public static final String ARTIFICIAL_WEBSITE = "artificial_website";

    //广点通广告的相关统计。
    public static final String GDTAD_SHOW = "gdtad_show";
    public static final String GDTAD_CLICK = "gdtad_click";
    public static final String GDTAD_REQUEST = "gdtad_request";
    public static final String GDTAD_REQUEST_VTWO = "gdtad_request_vtwo";
    public static final String GDTAD_ERROE = "gdtad_error";
    public static final String GDTAD_CLOSE = "gdtad_close";
    public static final String GDTAD_TIMEOUT = "gdtad_timeout";

    //百度广告的相关统计。
    public static final String BAIDUAD_SHOW = "baiduad_show";
    public static final String BAIDUAD_CLICK = "baiduad_click";
    public static final String BAIDUAD_REQUEST = "baiduad_request";
    public static final String BAIDUAD_REQUEST_VTWO = "baiduad_request_vtwo";
    public static final String BAIDUAD_ERROE = "baiduad_error";
    public static final String BAIDUAD_CLOSE = "baiduad_close";

    //服务端合作广告的相关统计。
    public static final String SERVERAD_SHOW = "serverad_show";
    public static final String SERVERAD_CLICK = "serverad_click";
    public static final String SERVERAD_CLOSE = "serverad_close";

    //柚子合作广告的相关统计。
    public static final String YZSERVERAD_SHOW = "youziad_show";
    public static final String YZSERVERAD_CLICK = "youziad_click";
    public static final String YZSERVERAD_CLOSE = "youziad_close";
    //互众合作广告的相关统计。
    public static final String HZSERVERAD_SHOW = "hzad_show";
    public static final String HZSERVERAD_CLICK = "hzad_click";
    public static final String HZSERVERAD_CLOSE = "hzad_close";
    public static final String HZPREROLLS_SHOW = "hzprerolls_show";
    public static final String HZPEROLLS_CLICK = "hzprerolls_click";

    //讯飞合作广告的相关统计。
    public static final String XFSERVERAD_SHOW = "xfad_show";
    public static final String XFSERVERAD_CLICK = "xfad_click";
    public static final String XFSERVERAD_CLOSE = "xfad_close";

    //头条广告数据统计
    public static final String TTSERVERAD_SHOW = "ttad_show";
    public static final String TTSERVERAD_CLICK = "ttad_click";

    public static final String OPENAPP = "openapp";
    public static final String EXITBOX_SHOW = "exitbox_show";
    public static final String PLAYER_MAIN = "player_main";

    //快看广告友盟统计
    public static final String SERVER_SHOW = "serverad_show";
    public static final String SERVER_CLICK = "serverad_click";


    /**
     * 默认播放状态上报
     */
    public static void postDefaultPlayState(String defaultState) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("default_state", defaultState);
        doPost(ALBUM_PLAY_DEFAULT_STATE, postData);
    }

    /**
     * 上报播放数据
     *
     * @param aid          albumId
     * @param videoName    视频名
     * @param categoryName 分类名
     */
    public static void postPlay(String aid, String videoName,
                                String categoryName) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("aid", aid);
        postData.put("name", videoName);
        postData.put("categoryname", categoryName);
        doPost("play", postData);
    }

    /**
     * 上报专题页面数据
     *
     * @param themeName 专题名
     */
    public static void postThemeClick(String themeName) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("themename", themeName);
        doPost("themeclick", postData);
    }

    /**
     * 上报播放器数据
     */
    public static void postPlayer(UMengReport uMengReport) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("albumID", uMengReport.getAid());
        postData.put("vid", uMengReport.getVid());
        postData.put("name", uMengReport.getName());
        postData.put("from", uMengReport.getFrom());
        postData.put("playresult", uMengReport.getPlayresult());
        postData.put("h5", uMengReport.getIsUserH5());
        postData.put("category", uMengReport.getCategoryName());
        doPost("player", postData);
    }

    /**
     * 上报频道页点击
     *
     * @param channel 上报页面名
     */
    public static void postChannelClick(String channel) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("channel", channel);
        doPost("channelclick", postData);
    }

    /**
     * 上报焦点图点击
     *
     * @param focusnum 焦点图序号
     * @param type     焦点图类型
     * @param name     焦点图名
     */
    public static void postFocusClick(int focusnum, String type, String name) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("focusnum", Integer.toString(focusnum));
        postData.put("type", type);
        postData.put("name", name);
        doPost("focusclick", postData);
    }

    /**
     * 上报推荐位点击
     *
     * @param recomIndex 推荐序号
     * @param recomName  推荐栏名
     * @param name       推荐名
     */
    public static void postRecomClick(int recomIndex, String recomName,
                                      String name) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("recomname", recomName);
        postData.put("name", name);
        StringBuilder sb = new StringBuilder(recomName);
        sb.append("-");
        sb.append(recomIndex);
        postData.put("recomnum", sb.toString());
        doPost("recomclick", postData);
    }

    /**
     * 添加下载点击
     *
     * @param name       名字
     * @param albumID    专辑ID
     * @param vid        单集ID
     * @param newstate   是否添加成功
     * @param failreason 失败原因
     */
    public static void postAddDownLoadClick(String name, String albumID, String vid, String newstate, String failreason) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("name", name);
        postData.put("albumID", albumID);
        postData.put("vid", vid);
        postData.put("newstate", newstate);
        postData.put("failreason", failreason);

        doPost("downloadstart", postData);
    }

    /**
     * 添加下载状态
     *
     * @param name      名字
     * @param albumID   专辑ID
     * @param vid       单集ID
     * @param taskstate 任务状态
     */
    public static void postDownLoadStateClick(String name, String albumID, String vid, String taskstate) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("name", name);
        postData.put("albumID", albumID);
        postData.put("vid", vid);
        postData.put("taskstate", taskstate);

        doPost("downloadstate", postData);
    }

    /**
     * 推送获得上报
     *
     * @param type 专辑: album; 专题: theme; 页面: link
     */
    public static void postPush(String type) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("type", type);
        doPost("push", postData);
    }

    /**
     * 推送点击上报
     *
     * @param type 专辑: album; 专题: theme; 页面: link
     */
    public static void postPushClick(String type) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("type", type);
        doPost("pushclick", postData);
    }

    /**
     * H5分享上报
     *
     * @param url H5链接: title; 标题: shareto; 微信或qq: succeed 是否成功, type 分享页面
     */
    public static void postH5Share(String url, String title, String shareto, String succeed, String type) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("url", url);
        postData.put("title", title);
        postData.put("share_to", shareto);
        postData.put("succeed", succeed);
        postData.put(type, type);
        doPost(H5_SHARE, postData);
    }

    public static void postPageVisitTime(String tag, String from) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("from", from);
        doPost(tag, postData);
    }

    public static void postPageShow(String tag, String name, String from) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("name", name);
        postData.put("from", from);
        doPost(tag, postData);
    }

    public static void postVideoDetailVisitTime(String name, String from, String category) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("from", from);
        postData.put("name", name);
        postData.put("category", category);
        doPost("page_show_details", postData);
    }

    public static void postPageLeaveTime(String tag) {
        doEventPost(tag);
    }

    public static void postWatchTime(String name, String from, String category, int duration) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("from", from);
        postData.put("name", name);
        postData.put("category", category);
        doPostDuration(WATCH_TIME, postData, duration);
    }

    public static void postPlayerPause(String mAdposition){
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.PLAYER_PAUSE, gdtPropertyMap);
    }


    /**
     * 时长上报通用方法
     */

    public static void doPostDuration(String tag, HashMap<String, String> postData, int duration) {
        if (ENABLE) {
            Context mContext = MoviesApplication.getInstance()
                    .getApplicationContext();
            MobclickAgent.onEventValue(mContext, tag, postData, duration);
        }
    }

    /**
     * 上报通用方法
     *
     * @param tag
     * @param postData
     */
    public static void doPost(String tag, HashMap<String, String> postData) {
        if (ENABLE) {
            Context mContext = MoviesApplication.getInstance()
                    .getApplicationContext();
            MobclickAgent.onEvent(mContext, tag, postData);
        }
    }

    public static void doPost(String tag, String key, String value) {
        if (ENABLE) {
            HashMap<String, String> postData = new HashMap<>();
            postData.put(key, value);
            Context mContext = MoviesApplication.getInstance()
                    .getApplicationContext();
            MobclickAgent.onEvent(mContext, tag, postData);
        }
    }


    /**
     * 云盘详情页
     *
     * @param name 名字
     */
    public static void postCloudDetail(String name) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("name", name);
        doPost("clouddetail", postData);
    }

    /**
     * 云盘-点击添加下载
     *
     * @param name 名字
     */
    public static void postCloudDownload(String name) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("name", name);
        doPost("clouddownload", postData);
    }

    /**
     * 云盘-分享上报
     *
     * @param name       名字
     * @param actionName 分享至微信朋友圈   事件名：cloudshareweixinmoments
     *                   分享至微信好友       事件名：cloudshareweixin
     *                   分享至微博              事件名：cloudshareweibo
     *                   分享至QQ      事件名：cloudshareqq
     */
    public static void postCloudShare(String name, String actionName) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("name", name);
        doPost(actionName, postData);
    }

    /**
     * 自定义事件上报
     *
     * @param tag
     */
    public static void doEventPost(String tag) {
        if (ENABLE) {
            Context mContext = MoviesApplication.getInstance()
                    .getApplicationContext();
            LogUtils.e(TAG, "umeng key is " + tag);
            MobclickAgent.onEvent(mContext, tag);
        }
    }

    /**
     * 详情页站点源点击
     *
     * @param siteName 选择站点源名字
     */
    public static void postWebSiteClick(String siteName) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("siteName", siteName);
        doPost("website", postData);
        LogUtils.e(TAG, "websitepost item is " + siteName);
    }

    /**
     * 下载页广告展示时间
     *
     * @param times 广告展示时间区间
     */
    public static void postDownLoadTime(String times) {
        HashMap<String, String> postData = new HashMap<String, String>();
        postData.put("time", times);
        doPost("download_page_stay", postData);
    }
}
